package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.tengju.bff.interfaces.shared.*;
import com.tengju.data.domain.model.clickhouse.BrandAnalysisM;
import com.tengju.data.domain.model.clickhouse.BrandId;
import com.tengju.data.domain.model.clickhouse.DwdCrmDetailMRepository;
import com.tengju.data.domain.shared.Month;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/3/18
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BrandAnalysisFacade {

    private final DwdCrmDetailMRepository dwdCrmDetailMRepository;

    /**
     * 获取俱乐部月度品牌市场指数分析
     * @param month
     * @param clubId
     * @return
     */
    public TableVO brand(Integer month, Long clubId) {
        List<BrandAnalysisM> brandAnalysisMList = dwdCrmDetailMRepository.listBrand(new Month(month),clubId);
        if (CollectionUtils.isEmpty(brandAnalysisMList)) {
            return null;
        }
        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("rank", "排名", false),
                ColumnVO.of("brandName", "品牌", false),
                ColumnVO.of("mMoneyOut", "市场指数", false));
        List<Map<String, Object>> rows = new ArrayList<>();
        brandAnalysisMList = brandAnalysisMList.stream()
                .sorted(Comparator.comparing(BrandAnalysisM::getMMoneyOut,Comparator.reverseOrder())
                        .thenComparing(Comparator.comparing(BrandAnalysisM::getBrandName))
                )
                .collect(Collectors.toList());
        int rank = 1;
        for (BrandAnalysisM brandAnalysisM : brandAnalysisMList) {
            Map<String, Object> row = new HashMap<>();
            row.put("rank",RankVO.of(rank++));
            row.put("brandId",brandAnalysisM.getBrandId().getValue());
            row.put("brandName",brandAnalysisM.getBrandName());
            row.put("mMoneyOut",new AmountVO(AmountType.THOUSAND,brandAnalysisM.getMMoneyOut()));
            rows.add(row);
        }
        table.setHeader(columns);
        table.setRecords(rows);
        return table;
    }


    public static void main(String[] args) {
        new BrandAnalysisFacade(null).run();
    }

    public void run() {
        List<BrandAnalysisM> brandAnalysisMList = List.of(
                new BrandAnalysisM(
                        new BrandId(1),
                        "v",
                        BigDecimal.valueOf(200),
                        null,
                        null
                ),
                new BrandAnalysisM(
                        new BrandId(2),
                        "a",
                        BigDecimal.valueOf(500),
                        null,
                        null
                )
        );
        brandAnalysisMList = brandAnalysisMList.stream()
                .sorted(Comparator.comparing(BrandAnalysisM::getMMoneyOut,Comparator.reverseOrder())
                        .thenComparing(Comparator.comparing(BrandAnalysisM::getBrandName))
                )
                .collect(Collectors.toList());
        System.out.println(brandAnalysisMList.size());
    }

}
